<?php
include("conn.php");

$code = $_POST['code'];

$url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$sessionKey}&js_code={$code}&grant_type=authorization_code";
$resp = file_get_contents($url);
$robot = json_decode($resp);
$openid = $robot->openid;
$sql = "SELECT * FROM `ut_user` WHERE `openid` = '$openid'";
$query = $conn->query($sql);
if ($row = mysqli_fetch_assoc($query)) {
  print_r(json_encode($row));
} else {
  print_r(json_encode(null));
}
$query->free_result();
$conn->close();
?>